package com.wxmp.wxapi.util;

import com.wxmp.wxcms.domain.BookDto;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;
import java.net.URL;
import java.rmi.ServerException;

/**
 * @Author zzm
 * @Date 2023/8/15 17:03
 * @Version 1.0
 * @Description TODO
 */
@Component
public class JsoupUtil {
    private static RedisTemplate<String,Object> redisTemplate;
    private static final String url1 = "https://wap.ishuquge.la/search.php";
    private static final String url2 = "https://wap.ishuquge.la";
    private static final String BOOK_KEY = "%s_%s_next";

    @Resource
    public void setRedisTemplate(RedisTemplate<String,Object> redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    public static String getContent(BookDto bookDto) throws IOException{
        String url3 = null;
        String url4 = null;
        String url5 = null;
        int contentTag = bookDto.getTag();
        String name = bookDto.getName();
        String key = String.format(BOOK_KEY, name, contentTag);
        Object content = redisTemplate.opsForValue().get(key);
        if (content != null) {
            return getText(url2+content,key);
        }
        //获取请求连接
        Connection con = Jsoup.connect(url1);
        //添加参数
        con.data("searchkey", name);
        con.header("User-Agent","Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36");
        Document doc = con.post();
        Elements bookbox = doc.select(".bookbox");
        if (bookbox != null) {
            for (Element element : bookbox) {
                Elements booknames = element.select(".bookname");
                String bookId = null;
                boolean tag = false;
                if (booknames != null) {
                    for (Element bookname : booknames) {
                        String text = bookname.text();
                        if (name.equals(text)) {
                            tag = true;
                            Elements a = bookname.select("a");
                            bookId = a.attr("href");
                            break;
                        }
                    }
                }
                if (tag) {
                    if (bookId != null) {
                        url3 = url2 + bookId;
                    }else {
                        throw new ServerException("没有id");
                    }
                }
            }
            int y = 0;
            url5 = getBookConTent(url3, url4, url5, contentTag,y);
            if (url5 != null) {
                return getText(url5, key);
            }
        }
        return null;
    }

    private static String getBookConTent(String url3, String url4, String url5, int contentTag,int y) throws IOException {
        if (url3 != null) {
            //获取目录
            Document document = Jsoup.parse(new URL(url3), 30000);
            Elements elements = document.getElementsByTag("option");
            if (y > 0) {
                String value = elements.get(y+1).attr("value");
                url4 = url2 + value;
            }else {
                for (int i = 0; i < elements.size(); i++) {
                    String text = elements.get(i).text();
                    //匹配章节区间
                    if(VerifyUtil.extract(text, contentTag)){
                        y = i;
                        String value = elements.get(i).attr("value");
                        url4 = url2 + value;
                        break;
                    }
                }
            }
        }
        if (url4 != null) {
            Document parse = Jsoup.parse(new URL(url4), 30000);
            Elements chapter = parse.getElementsByTag("li");
            for (int i = 0; i < chapter.size(); i++) {
                String text = chapter.get(i).text();
                int x = VerifyUtil.strExtract(text);
                if (x > 0 && contentTag == x) {
                    Elements elements1 = chapter.get(i).getElementsByTag("a");
                    String html1 = elements1.attr("href");
                    url5 = url2 + html1;
                    break;
                }else if(i == chapter.size() - 1){
                    return getBookConTent(url3, url4, url5, contentTag,y);
                }
            }
        }
        return url5;
    }


    private static String getText(String url5, String key) throws IOException {
        Document parse = Jsoup.parse(new URL(url5), 30000);
        Element element = parse.getElementById("nr1");
        Element pbNext = parse.getElementById("pb_next");
        if (pbNext != null) {
            String attr = pbNext.attr("href");
            redisTemplate.opsForValue().set(key,attr);
        }
        return element.text();
    }

}
